import React from "react";
import { FlatList, View, Text, TouchableOpacity } from "react-native";
import DataResource from "../module/DataResource";
import {
  ExListener,
  BProps,
  BState,
  EnoadNotifaction,
  HeaderView,
  BaseComponent,
} from "../../index";
import { R } from '../../../core';

export interface IProps extends BProps {
  data?: any;
}

export default class ObjectList
  extends BaseComponent<IProps, BState>
  implements ExListener {
  constructor(props: IProps) {
    super(props);
    this.initProps();
  }

  /**
   * 初始化的方法
   */
  initProps() {
    //注册通知监听
    EnoadNotifaction.getInstance().addListener(this);
    //初始化数据
    this.props.actions.init(
      this.props.componentId,
      new DataResource("ObjectList")
    );
  }

  componentWillUnmount() {
    EnoadNotifaction.getInstance().delListener(this);
  }

  /**
   * 渲染每一行
   * @param data 行数据
   * @returns 返回View
   */
  _renderItem = (data: any) => {
    return (
      <TouchableOpacity
        style={R.styles.row}
        onPress={() =>
          this.props.actions &&
          this.props.actions.open(this.props.componentId, data.item)
        } //每一行的点击效果
      >
        <Text style={R.styles.text16}>{data.item.name}</Text>
      </TouchableOpacity>
    );
  };

  render() {
    let data = this.props.state.getIn([
      "instances",
      this.props.componentId,
      "data",
    ]);
    return (
      <View style={{ flex: 1, backgroundColor: R.color.white }}>
        <HeaderView
          componentId={this.props.componentId}
          title={this.props.title}
        />
        {data ? (
          <FlatList
            data={data}
            keyExtractor={(item) => item.uuid.toString()}
            renderItem={this._renderItem}
          />
        ) : null}
      </View>
    );
  }

  /**
   * 获取该对象的唯一识别信息
   * @returns 返回唯一识别信息
   */
  getTag(): string {
    return "ObjectList";
  }
  /**
   * 收到发送的通知信息
   * @param param 通知信息
   */
  init(param: string) {
    console.log(param);
  }
}
